<?php
namespace Cyfy\Modules\Objects;

/**
 * Object, which can be slided up into a link and slided back down.
 */
class Collapsible extends GeneralObject
{
    /**
     * Is collapsed?
     * @var bool
     */
    private $collapsed;
    
    /**
     * Creates a new object
     * @param string Custom class name
     */
    function __construct($cls = '')
    {
        $this -> cls = $cls;
        \Cyfy\Cyfy ::  css(\Cyfy\Cyfy ::  getSourcePath("Cyfy/Modules/Objects/collapsible.css"));   
    }
    
    /**
     * Sets data
     * @param string Name of the object
     * @param string Data
     * @param bool   Is Collapsed?
     */
    function setData($name, $data = "&nbsp;", $collapsed = FALSE){
        $this -> data = $data;
        $this -> name = $name;
        $this -> collapsed = $collapsed;
    }
    
    /** 
     * Returns the object as a string
     * @return string
     */
    function show()
    {
        $unique = uniqid();
        $output = "<p class=\"collapsible-controller\"><a href=\"#\" id=\"collapsible-controller-{$unique}\" class=\"{$this->cls}Controller\" onclick=\"$('#collapsible-{$unique}').slideToggle('fast'); return false;\">" .$this -> name ."</a></p> \n";
        $output .= "<div class=\"collapsible {$this->cls}\" id=\"collapsible-{$unique}\"> \n";
        $output .= $this -> data ." \n";
        $output .= "</div> \n";
        if($this -> collapsed){
            $output .= \Cyfy\Cyfy ::  javascript("$(\"#collapsible-{$unique}\").hide(1);");
        }
        return $output;
    }
}